/*
 * RoboEye - Web client for www.newocr.com that allows you
 * to recognize an image captured by the camera.
 *
 * Copyright (C) Oxygen Development, 2011
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package com.devoxy.roboeye;

import android.app.Application;
import android.content.Context;
import com.devoxy.roboeye.global.ContextHolder;
import com.devoxy.roboeye.global.PreferencesHelper;
import com.devoxy.roboeye.global.log.Log;

import java.io.File;

/**
 * Created by Oxygen Development.
 * Author: Dmitriy Tarasov (dm.vl.tarasov@gmail.com)
 * Date: 16.06.11
 * Time: 23:45
 */
public class RoboEyeApplication extends Application {

    private static final String TAG = RoboEyeApplication.class.getName();

    @Override
    public void onCreate() {
        // Attention! ContextHolder must be inited before all
        ContextHolder holder = ContextHolder.getInstance();
        holder.setContext(getBaseContext());
        initLogging();
        initCrashReporting();
        PreferencesHelper.initPreferences();
        initDir();
    }

    @Override
    public Context getApplicationContext() {
        return getBaseContext();
    }

    private void initCrashReporting() {
        Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread thread, Throwable ex) {
                PreferencesHelper.setPref(R.string.is_app_crashed_pref, true);
                // TODO send report on email address
                Log.e(TAG, ">>>>>>>>>>>>>> Application crashed >>>>>>>>>>>>", ex);
                System.exit(-1);
            }
        });
    }

    private void initLogging() {
        // TODO setup logging level
        //Log.i("MYTAG", "####################>>>>>>>>>>>>>>>>>>>>>>>>> Disable logging");
        //System.setProperty("log.tag.MYTAG", String.valueOf(Log.ERROR));
        //com.devoxy.roboeye.global.log.Log.i("MYTAG", "####################>>>>>>>>>>>>>>>>>>>>>>>>> Logging disabled");
    }

    private void initDir() {
        String pathToPhotos = PreferencesHelper.pathToPhotos();
        File photos = new File(pathToPhotos);
        if (!photos.exists()) {
            photos.mkdirs();
        }

        String pathToResults = PreferencesHelper.pathToResults();
        File results = new File(pathToResults);
        if (!results.exists()) {
            results.mkdirs();
        }
    }
}
